#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "usermanager.h"
#include <QTextEdit>
#include <QMdiSubWindow>

#include "subwindows/expirycheck.h"
#include "subwindows/medicineadd.h"
#include "subwindows/medicinedelete.h"
#include "subwindows/medicinequery.h"
#include "subwindows/medicineupdate.h"
#include "subwindows/medicinetake.h"
#include "subwindows/purchaseordercreate.h"
#include "subwindows/purchaserecordquery.h"
#include "subwindows/salesrecordquery.h"
#include "subwindows/stockcheck.h"
#include "subwindows/userdelete.h"
#include "subwindows/userregister.h"
#include "subwindows/userupdate.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , mainwindowUi(new Ui::MainWindow)
    , userManagementMenu(nullptr)
    , medicineManagementMenu(nullptr)
    , inventoryManagementMenu(nullptr)
    , orderManagementMenu(nullptr)
{
    mainwindowUi->setupUi(this);
    setupMenus();

    ts = this;
}

MainWindow::~MainWindow()
{
    delete mainwindowUi;
}

void MainWindow::setupMenus()
{
    QMenuBar *menuBar = mainwindowUi->menubar;

    if (UserManager::instance().getRole() == UserManager::Admin) {
        userManagementMenu = menuBar->addMenu(tr("用户管理"));
        userManagementMenu->addAction(tr("用户注册"), this, &MainWindow::onUserRegisterActionCliked);
        userManagementMenu->addAction(tr("用户删除"), this, &MainWindow::onUserDeleteActionCliked);
        userManagementMenu->addAction(tr("更新用户信息"), this, &MainWindow::onUserUpdateActionCliked);

        medicineManagementMenu = menuBar->addMenu(tr("药品管理"));
        medicineManagementMenu->addAction(tr("药品添加"), this, &MainWindow::onMedicineAddActionCliked);
        medicineManagementMenu->addAction(tr("药品删除"), this, &MainWindow::onMedicineDeleteActionCliked);
        medicineManagementMenu->addAction(tr("药品更新"), this, &MainWindow::onMedicineUpdateActionCliked);
        medicineManagementMenu->addAction(tr("药品查询"), this, &MainWindow::onMedicineQueryActionCliked);

        inventoryManagementMenu = menuBar->addMenu(tr("库存管理"));
        inventoryManagementMenu->addAction(tr("库存预警"), this, &MainWindow::onStockCheckActionCliked);
        inventoryManagementMenu->addAction(tr("过期检测"), this, &MainWindow::onExpiryCheckActionCliked);

        orderManagementMenu = menuBar->addMenu(tr("订单管理"));
        orderManagementMenu->addAction(tr("创建采购订单"), this, &MainWindow::onPurchaseOrderCreateActionCliked);
        orderManagementMenu->addAction(tr("查询采购订单"), this, &MainWindow::onPurchaseRecordQueryActionCliked);
        orderManagementMenu->addAction(tr("药品销售记录"), this, &MainWindow::onSalesRecordQueryActionCliked);
    }
    else if (UserManager::instance().getRole() == UserManager::User) {
        userManagementMenu = menuBar->addMenu(tr("用户管理"));
        userManagementMenu->addAction(tr("更新信息"), this, &MainWindow::onUserUpdateActionCliked);

        medicineManagementMenu = menuBar->addMenu(tr("药品管理"));
        medicineManagementMenu->addAction(tr("取药"), this, &MainWindow::onMedicineTakeActionCliked);

        orderManagementMenu = menuBar->addMenu(tr("订单管理"));
        orderManagementMenu->addAction(tr("购买记录"), this, &MainWindow::onSalesRecordQueryActionCliked);
    }
    else {
        QT_THROW("unknown user role!");
    }
}

QMdiSubWindow* MainWindow::createSubWindow(QWidget* widget)
{
    QMdiSubWindow *subWindow = new QMdiSubWindow;
    subWindow->setWidget(widget);
    subWindow->setAttribute(Qt::WA_DeleteOnClose);
    QSize subWindowSize(widget->frameSize());
    subWindow->resize(subWindowSize);
    mainwindowUi->mdiArea->addSubWindow(subWindow);
    return subWindow;
}

void MainWindow::onUserRegisterActionCliked()
{
    createSubWindow(new UserRegisterWindow(this))->show();
}

void MainWindow::onUserDeleteActionCliked()
{
    createSubWindow(new UserDeleteWindow(this))->show();
}

void MainWindow::onUserUpdateActionCliked()
{
    createSubWindow(new UserUpdateWindow(this))->show();
}

void MainWindow::onMedicineAddActionCliked()
{
    createSubWindow(new MedicineAddWindow(this))->show();
}

void MainWindow::onMedicineDeleteActionCliked()
{
    createSubWindow(new MedicineDeleteWindow(this))->show();
}

void MainWindow::onMedicineUpdateActionCliked()
{
    createSubWindow(new MedicineUpdateWindow(this))->show();
}

void MainWindow::onMedicineQueryActionCliked()
{
    createSubWindow(new MedicineQueryWindow(this))->show();
}

void MainWindow::onStockCheckActionCliked()
{
    createSubWindow(new StockCheckWindow(this))->show();
}

void MainWindow::onExpiryCheckActionCliked()
{
    createSubWindow(new ExpiryCheckWindow(this))->show();
}

void MainWindow::onPurchaseOrderCreateActionCliked()
{
    createSubWindow(new PurchaseOrderCreateWindow(this))->show();
}

void MainWindow::onSalesRecordQueryActionCliked()
{
    createSubWindow(new SalesRecordQueryWindow(this))->show();
}

void MainWindow::onMedicineTakeActionCliked()
{
    createSubWindow(new MedicineTakeWindow(this))->show();
}

void MainWindow::onPurchaseRecordQueryActionCliked()
{
    createSubWindow(new PurchaseRecordQueryWindow(this))->show();
}
